package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.juyu.miliao.api.util.RedisKeys;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Repository
public class UserCallRedisDao {
	@Resource
	private StringRedisTemplate stringRedisTemplate;

	public void startHostCall(long userId) {
		stringRedisTemplate.opsForSet().add(RedisKeys.SET_HOSTCALL.getValue(), userId+"");
	}

	public void endHostCall(long userId) {
		stringRedisTemplate.opsForSet().remove(RedisKeys.SET_HOSTCALL.getValue(), userId+"");
	}
	
	public Boolean findHostCall(String userId){
		return stringRedisTemplate.opsForSet().isMember(RedisKeys.SET_HOSTCALL.getValue(), userId);
	}

	public Set<String> getHostCall() {
		return stringRedisTemplate.opsForSet().members(RedisKeys.SET_HOSTCALL.getValue());
	}

	/**
	 * 获取主播打招呼次数
	 * @param userId
	 * @param localDate
	 * @param type 0:默认，
	 * @return
	 */
	public int getGreetNumByUserId(long userId, LocalDate localDate, int type) {

		String key = RedisKeys.M_GRAD_LEVEL_BY_USER_ID_NUMBER
				.getValue(localDate.toString(),Long.toString(userId),0);

		long  num =stringRedisTemplate.opsForValue().increment(key,0);

		long expireTime = DateUtil.getTheRemainingTimesOfTheDay(LocalDateTime.now().plusDays(1).withHour(0)
				.withMinute(0).withSecond(0).withNano(0), TimeUnit.MINUTES);

		stringRedisTemplate.expire(key,expireTime,TimeUnit.MINUTES);

		return (int) num;
	}

	public Set<String> getCallUserSet(long userId, LocalDate localDate) {
		String key = RedisKeys.CALL_USER_BY_HOST.getValue(localDate.toString(),Long.toString(userId));
		Set<String> set=stringRedisTemplate.opsForSet().members(key);
		if(set!=null){
			return set;
		}else{
			return new HashSet<>();
		}
	}

	public void setCallUserSet(long userId,LocalDateTime localDateTime,List<String> userIdList) {

		String key = RedisKeys.CALL_USER_BY_HOST.getValue(localDateTime.toLocalDate().toString(),Long.toString(userId));

		for(String value:userIdList) {
			stringRedisTemplate.opsForSet().add(key, value);
			long expireTime = DateUtil.getTheRemainingTimesOfTheDay(localDateTime.plusDays(1).withHour(0)
					.withMinute(0).withSecond(0).withNano(0), TimeUnit.MINUTES);
			stringRedisTemplate.expire(key,expireTime,TimeUnit.MINUTES);
		}
	}

	public boolean checkUserId(long userId, String id) {
		String key = RedisKeys.CALL_USER_BY_HOST.getValue(LocalDate.now().toString(),Long.toString(userId));
		boolean bl = stringRedisTemplate.opsForSet().isMember(key,id);
		return bl;
	}


	public void incrementCallUserNumber(long userId, LocalDateTime localDateTime,int type) {
		String key = RedisKeys.M_GRAD_LEVEL_BY_USER_ID_NUMBER
				.getValue(localDateTime.toLocalDate().toString(),Long.toString(userId),0);

		stringRedisTemplate.opsForValue().increment(key,1);

		long expireTime = DateUtil.getTheRemainingTimesOfTheDay(LocalDateTime.now().plusDays(1).withHour(0)
				.withMinute(0).withSecond(0).withNano(0), TimeUnit.MINUTES);

		stringRedisTemplate.expire(key,expireTime,TimeUnit.MINUTES);

	}
}
